package org.xhy.retryer.strategy;

import org.xhy.retryer.core.RetryerSupplier;

import java.util.concurrent.TimeUnit;

/**
 * @description: 根据重试时间
 * @gitee: https://gitee.com/XhyQAQ
 * @copyright: B站: https://space.bilibili.com/152686439
 * @Author: Xhy
 * @CreateTime: 2024-04-29 21:39
 */
public class TimeRetryerStrategy extends RetryerStrategy{

    private final int maxCount;

    public TimeRetryerStrategy(int count,long time, TimeUnit timeUnit) {
       super(count,time,timeUnit);
        this.maxCount = count;
    }

    @Override
    public void reset() {
        this.count = maxCount;
    }

    @Override
    boolean exec() {

        while (count>0){
            count--;
            try {
                timeUnit.sleep(time);
                return true;
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new RuntimeException("Retry interrupted", e);
            }
        }


        return false;
    }
}
